package stars.starSystems;

import eventManager.EventManager;
import galaxies.enums.EGalaxyAge;
import gameAppLayer.GameSettings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import old.coordinate.CoordinateModel;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;


import stars.enums.StarSystemDistributions;
import stars.enums.StarSystemNames;
import utilities.Key;
import utilities.MVCMaker;
import view.EViewList;
import view.AbstractView;
import view.IView;

/*
 * Call factories to create a single starSystem:  controllers, models, and views
 * Names and layouts of systems are chosen at random
 * 
 * Inputs:  List of system locations, GameSettings
 * Ouputs:  ArrayList<Controller> controllers for generated star systems
 */

public class StarSystemGenerator {

	private static final Logger logger = Logger.getLogger(StarSystemGenerator.class.getName());
	
	private StarSystemModelBuilder modelFact;
	private StarSystemModel model;
	
	private StarSystemViewBuilder viewFact;
	//private HashMap<EViewList,AbstractView> tempView;
	private Spatial view;
	
	//private FCStarSystem controllerFact;
	private StarSystemControllerBuilder controllerFact;
	private StarSystemController controller;
	
	private GameSettings settings; //QQQ Not needed?
	private AssetManager assetManager;
	private EventManager eventManager;
	private StarSystemDistributions systemDists;
	private StarSystemNames systemNames;
	private StarSystemLayoutBuilder systemLayoutBuilder = new StarSystemLayoutBuilder();
	
	public StarSystemModel systemModel;
	
	public StarSystemGenerator(GameSettings settings, AssetManager assets, 
			EventManager eventManager, StarSystemDistributions systemDists,
			StarSystemNames systemNames) {
		this.settings = settings;
		this.assetManager = assets;
		this.eventManager = eventManager;
		this.systemDists = systemDists;
		this.systemNames = systemNames;
	}
	
	public StarSystemController createStarSystem(ArrayList<Integer> location) {
		
		logger.log(Level.INFO, "createStarSystem");
		
		modelFact = new StarSystemModelBuilder();
		viewFact = new StarSystemViewBuilder();
		controllerFact = new StarSystemControllerBuilder();
		
		model = modelFact.createModel(location, systemDists, systemNames);
		view = viewFact.buildView(assetManager, model);
		controller = controllerFact.createController(eventManager, model, view);//new StarSystemController(events, spatial);
		
		MVCMaker.linkElements(model, view, controller);
		
		systemModel = model;
		
		return controller;
	}
	
	public ArrayList<StarSystemController> createStarSystems(ArrayList<ArrayList<Integer>> locations) {
		
		logger.log(Level.INFO, "createStarSystems");
		
		ArrayList<StarSystemController> systems = new ArrayList<StarSystemController>();
		
		modelFact = new StarSystemModelBuilder();
		viewFact = new StarSystemViewBuilder();		
		//String systemType;
		for (ArrayList<Integer> location: locations) {
			//systemType = systemLayoutBuilder.chooseLayout(systemDists);
			
			model = modelFact.createModel(location, systemDists, systemNames);
			view = viewFact.buildView(assetManager, model);
			controller = new StarSystemController(eventManager, model, view);
			
			MVCMaker.linkElements(model, view, controller);
			
			//Add to system list
			systems.add(controller);
		}
		
		return systems;
	}
	
	/*
	 * Build a starSystem object from an existing model.
	 * Used for loading from XML save files.
	 */
	public ArrayList<StarSystemController> loadStarSystems(ArrayList<StarSystemModel> models) {
		logger.log(Level.INFO, "loadStarSystem");
		
		ArrayList<StarSystemController> systems = new ArrayList<StarSystemController>();
		viewFact = new StarSystemViewBuilder();
		
		for (StarSystemModel model: models) {
			view = viewFact.buildView(assetManager, model);
			controller = new StarSystemController(eventManager, model, view);
			
			MVCMaker.linkElements(model, view, controller);
			
			systems.add(controller);
		}
		
		
		return systems;
	}
	
	
}
